#!/bin/bash
set -e

# pull from github
mkdir wtask_src
cd wtask_src
git clone https://github.com/LonlyWinter/wTask .

# WTASK_ROOT
dir_wtask_root="${HOME}/wtask"
mkdir -p ${dir_wtask_root}/bin

# build & mv
for app in bridge install machine cli web;
do
    cargo build --release --bin wtask_${app}
    mv target/release/wtask_${app} ${dir_wtask_root}/bin/
done
mv LICENSE ${dir_wtask_root}/
mv README.md ${dir_wtask_root}/

# set env: WTASK_ROOT/PATH
# echo "export WTASK_ROOT=$\{HOME\}/wtask" >> ~/.bashrc
# echo "export PATH=$\{PATH\}:$\{WTASK_ROOT\}/bin" >> ~/.bashrc

# start
echo "start ..."
cd ${dir_wtask_root}
for app in bridge machine web;
    ${dir_wtask_root}/bin/wtask_${app} --noterminal &
done
(crontab -l | grep -v "wtask_install"; echo "0 3 * * 0 ${dir_wtask_root}/bin/wtask_install") | crontab

echo "done"